package com.xijia.wuyouwallet.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

/**
 * 缓存工具类
 * <p>
 * <p>
 * Created by Administrator on 2016/11/18 0018.
 */

public class SharedpreferncesUtil {
    public static final String USER_GUIDE_FILE_NAME = "guide";//引导界面文件名
    public static final String KEY_USER_INFO = "user";

    /**
     * 设置进入过引导界面
     *
     * @param context
     */
    public static void setGuided(Context context) {
        SharedPreferences prefe = context.getSharedPreferences(USER_GUIDE_FILE_NAME, 0);
        SharedPreferences.Editor editor = prefe.edit();
        editor.putBoolean("isguide", true);
        editor.commit();

    }

    /**
     * 判断是否进入过引导界面
     *
     * @param context
     * @return
     */
    public static boolean getGuided(Context context) {
        SharedPreferences prefe = context.getSharedPreferences(USER_GUIDE_FILE_NAME, 0);
        boolean b = prefe.getBoolean("isguide", false);
        return b;
    }

    /**
     * 保存用户的登录密码
     *
     * @param context
     * @param userPhone
     * @param userPassWord
     */
    public static void setLogin(Context context, String userPhone, String userPassWord) {
        SharedPreferences preferences = context.getSharedPreferences(KEY_USER_INFO, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("UserPhone", userPhone);
        editor.putString("UserPassWord", userPassWord);
        editor.commit();
    }

    /**
     * 获取保存的登录密码
     *
     * @param context
     * @return
     */
    public static Map<String, String> getLoin(Context context) {
        Map<String, String> data = new HashMap<String, String>();
        SharedPreferences preferences = context.getSharedPreferences(KEY_USER_INFO, 0);
        data.put("UserPhone", preferences.getString("UserPhone", ""));
        data.put("UserPassWord", preferences.getString("UserPassWord", ""));
        return data;
    }

    public static void putBoolean(String key, boolean value, Context context) {
        SharedPreferences preferences = context.getSharedPreferences("config", context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    public static void putString(String key, String value, Context context) {
        SharedPreferences preferences = context.getSharedPreferences("config", context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(key, value);
        edit.commit();
    }

    public static void putInt(String key, int value, Context context) {
        SharedPreferences preferences = context.getSharedPreferences("config", context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putInt(key, value);
        edit.commit();
    }

    public static String getString(String key, String defValue, Context context) {
        SharedPreferences preferences = context.getSharedPreferences("config", context.MODE_PRIVATE);
        return preferences.getString(key, defValue);
    }

    public static boolean getBoolean(String key, boolean defValue, Context context) {
        SharedPreferences preferences = context.getSharedPreferences("config", context.MODE_PRIVATE);
        return preferences.getBoolean(key, defValue);
    }

    public static int getInt(String key, int defValue, Context context) {
        SharedPreferences preferences = context.getSharedPreferences("config", context.MODE_PRIVATE);
        return preferences.getInt(key, defValue);
    }

    public static void removeKey(String key, Context context) {
        SharedPreferences preferences = context.getSharedPreferences("config", context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.remove(key);
        edit.commit();
    }
}
